/**
 * Created by Nico on 2016/1/4.
 */

var Q = require('q');
var schema = require('../schema');
var async = require('async');
module.exports = {

    insert:function(args){
        var Model = schema.models[args.model];
        var deferred = Q.defer();
        console.log(args)
        Model.create(args.data,function(err,model){
            if(err){
                deferred.reject(err);
            }else{
                deferred.resolve(model);
            }
        });
        return deferred.promise;
    },
    updateOne:function(args){
        var Model = schema.models[args.model];
        var deferred = Q.defer();
        Model.findOne({where:args.where},function(err,model){
            if(err){
                deferred.reject(err);
            }else{
                model.updateAttributes(args.data,function(err,newModel){
                    if(err){
                        deferred.reject(err);
                    }else{
                        deferred.resolve(newModel);
                    }
                })

            }
        });
        return deferred.promise;
    },
    updateOneById:function(args){
        var Model = schema.models[args.model];
        var deferred = Q.defer();
        Model.find(args.where,function(err,model){
            if(err){
                deferred.reject(err);
            }else{
                model.updateAttributes(args.data,function(err,newModel){
                    if(err){
                        deferred.reject(err);
                    }else{
                        deferred.resolve(newModel);
                    }
                });
            }
        });
        return deferred.promise;
    },
    updateAll:function(args){
        var Model = schema.models[args.model];
        var deferred = Q.defer();
        Model.all({where:args.where},function(err,model){
            if(err){
                deferred.reject(err);
            }else{
                var count_err = 0,count_success = 0;
                async.eachSeries(model, function(eachModel,cb){
                    eachModel.updateAttributes(args.data,function(err,newModel){
                        if(err){
                            cb(null, count_err++);
                        }else{
                            cb(null, count_success++);
                        }
                    });
                },function(err){
                    if(err){
                        deferred.reject(err);
                    }else{
                        deferred.resolve({error:count_err,success:count_success});
                    }
                })
            }
        });
        return deferred.promise;
    },
    find:function(args){
        var Model = schema.models[args.model];
        var deferred = Q.defer();
        Model.find(args.where,function(err,model){
            if(err){
                deferred.reject(err);
            }else{
                deferred.resolve(model);
            }
        });
        return deferred.promise;
    },
    findOne:function(args){
        var Model = schema.models[args.model];
        var deferred = Q.defer();
        Model.findOne({where:args.where},function(err,model){
            if(err){
                deferred.reject(err);
            }else{
                deferred.resolve(model);
            }
        });
        return deferred.promise;
    },
    all:function(args){
        var Model = schema.models[args.model];
        var deferred = Q.defer();
        Model.all({where:args.where},function(err,model){
            if(err){
                deferred.reject(err);
            }else{
                deferred.resolve(model);
            }
        });
        return deferred.promise;
    },
    //删除
    destroy:function(args){
        var Model = schema.models[args.model];
        var deferred = Q.defer();
        Model.all({where:args.where},function(err,models){
            if(err){
                deferred.reject(err);
            }else{
                async.eachSeries(models,function(model,cb){
                    model.destroy(function(err,result){
                        if(err){
                            cb(null);
                        }else{
                            cb(null);
                        }
                    });
                },function(err){
                    if(err){
                        //回滚？事务？
                        deferred.reject({code:-1,msg:err});
                    }else{
                        deferred.resolve({code:0,msg:"success"});
                    }

                })

            }
        });
        return deferred.promise;
    },
    //清空表
    destroyAll:function(args){
        var Model = schema.models[args.model];
        var deferred = Q.defer();
        Model.destroyAll(function(err,model){
            if(err){
                deferred.reject(err);
            }else{
                deferred.resolve(model);
            }
        });
        return deferred.promise;
    },
    cityManagerList:function(){
        var deferred = Q.defer();
        var sql = "select a.id pid,a.province,b.id,b.city,b.status from gr_area a,gr_area b where a.id = b.parentid and a.parentid = 0 order by a.province";

        schema.adapter.query(sql, function(err,result){
            if(err){
                deferred.reject(err);
            }else{
                deferred.resolve(result);
            }
        });
        return deferred.promise;
    },
    //更新或者插入，如果id=data.id存在，则更新，否则插入
    upsert:function(args){
        var Model = schema.models[args.model];
        var deferred = Q.defer();
        Model.upsert(args.data,function(err,model){
            if(err){
                deferred.reject(err);
            }else{
                deferred.resolve(model);
            }
        });
        return deferred.promise;
    },
    findAndCount:function(args){
        var Model = schema.models[args.model];
        var deferred = Q.defer();

        Model.all(args.where,function(err,model){
            if(err){
                deferred.reject(err);
            }else{
                deferred.resolve(model);
            }
        });
        return deferred.promise;
    },
    findUserWithArea:function(){
        var User = schema.models.Ucenter_Member ;
        var Area = schema.models.Area ;
        var User_Area_Relations = schema.models.User_Area_Relation ;

        var deferred = Q.defer();
        User.all({include:{model:User_Area_Relations}},function(err,model){
            if(err){
                deferred.reject(err);
            }else{
                deferred.resolve(model);
            }
        });
        return deferred.promise;
    }
}